﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App21
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.DebugSettings.EnableFrameRateCounter = true;
            var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
            timer.Tick += (s, arg) =>
            {
                if (this.PauseButton.IsChecked.Value)
                    return;
                for (int i = 0; i < 10; i++)
                    Snow();
            };
            timer.Start();
        }

        Random _Random = new Random((int)DateTime.Now.Ticks);
        private void Snow()
        {
            var y = -100;
            var x = _Random.Next(-500, (int)LayoutRoot.ActualWidth - 100);
            var r = _Random.Next(90, 270);
            var scale = _Random.Next(1, 7) * .01;
            var flake = new Snowflake
            {
                HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
                VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top,
                RenderTransform = new CompositeTransform
                {
                    TranslateX = x,
                    TranslateY = y,
                    ScaleX = scale,
                    ScaleY = scale,
                    Rotation = r,
                },
            };
            LayoutRoot.Children.Add(flake);

            var d = TimeSpan.FromSeconds(_Random.Next(2, 5));

            y += (int)LayoutRoot.ActualHeight + 200;
            var animateY = new DoubleAnimation { To = LayoutRoot.ActualHeight, Duration = d };
            Storyboard.SetTarget(animateY, flake.RenderTransform);
            Storyboard.SetTargetProperty(animateY, "TranslateY");

            x += _Random.Next(100, 600);
            var animateX = new DoubleAnimation { To = x, Duration = d };
            Storyboard.SetTarget(animateX, flake.RenderTransform);
            Storyboard.SetTargetProperty(animateX, "TranslateX");

            r += _Random.Next(90, 360);
            var animateR = new DoubleAnimation { To = r, Duration = d };
            Storyboard.SetTarget(animateR, flake.RenderTransform);
            Storyboard.SetTargetProperty(animateR, "Rotation");

            var story = new Storyboard();
            story.Completed += (s, e) => LayoutRoot.Children.Remove(flake);
            story.Children.Add(animateX);
            story.Children.Add(animateY);
            story.Children.Add(animateR);
            story.Begin();
        }
    }
}
